<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>使用访问器保护数据</title>
</head>
<body>
    <script>
        'use strict'
        const user = {
            data : {name:"xiasnn",age:18},
            // setAge(value){
            //     if(typeof value != 'number' || value < 0 || value > 100){
            //         throw new Error('您输入的年龄格式是错误的');
            //     }
            //     this.data.age = value;
            // },
            // getAge(){
            //     console.log(this.data.age + "岁");
            // },
            set age(value){
                // 访问器：控制用户输入的数据，使其有一定的规范
                if(typeof value != 'number' || value < 0 || value > 100){
                    throw new Error('您输入的年龄格式是错误的');
                }
                this.data.age = value;
            },
            get age(){
                return this.data.age + "岁";
            }
        }
        // user.setAge(88);
        // user.getAge();
        user.age = 88;
        console.log(user.age);
    </script>
</body>
</html>